package com.cryptoneedle.garden.repository.garden.mapping;


import com.bubbles.engine.data.core.repository.BaseRepository;
import com.cryptoneedle.garden.entity.garden.mapping.MappingJoin;
import com.cryptoneedle.garden.key.JoinKey;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * <p>description: MappingJoin </p>
 *
 * @author CryptoNeedle
 * @date 2025-09-25
 */
@Repository
public interface MappingJoinRepository extends BaseRepository<MappingJoin, JoinKey> {

    @Query("FROM MappingJoin ORDER BY primaryCatalogName, primaryDatabaseName, primaryTableName, primaryColumnName, joinType, joinCatalogName, joinDatabaseName, joinTableName, joinColumnName")
    List<MappingJoin> mappingJoins();

    @Query("""
             FROM MappingJoin
            WHERE primaryCatalogName = :primaryCatalogName
              AND primaryDatabaseName = :primaryDatabaseName
            ORDER BY primaryCatalogName, primaryDatabaseName, primaryTableName, primaryColumnName, joinType, joinCatalogName, joinDatabaseName, joinTableName, joinColumnName
            """)
    List<MappingJoin> mappingJoins(String primaryCatalogName, String primaryDatabaseName);

    @Query("""
             FROM MappingJoin
            WHERE primaryCatalogName = :primaryCatalogName
              AND primaryDatabaseName = :primaryDatabaseName
              AND primaryTableName = :primaryTableName
            ORDER BY primaryCatalogName, primaryDatabaseName, primaryTableName, primaryColumnName, joinType, joinCatalogName, joinDatabaseName, joinTableName, joinColumnName
            """)
    List<MappingJoin> mappingJoins(String primaryCatalogName, String primaryDatabaseName, String primaryTableName);

    @Query("""
             FROM MappingJoin
            WHERE joinCatalogName = :joinCatalogName
              AND joinDatabaseName = :joinDatabaseName
              AND joinTableName = :joinTableName
            ORDER BY primaryCatalogName, primaryDatabaseName, primaryTableName, primaryColumnName, joinType, joinCatalogName, joinDatabaseName, joinTableName, joinColumnName
            """)
    List<MappingJoin> mappingJoinsByJoin(String joinCatalogName, String joinDatabaseName, String joinTableName);
}